// pages/server_end/schedule/schedule.js
const app = getApp();
const request = require('../../../utils/request.js');
const url = require('../../../utils/url.js');
const cookie = require('../../../utils/cookie.js');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    list: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
    nindex: 0,
    datelist:[],
    timelist:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    cookie.getCookie('userInfo', function (res) {
      if (res) {
        var etime = Date.parse(new Date()) / 1000;
        
        if (etime < res.expiretime) {
          that.setData({
            login: 1,
            token:res.token
          })
          that.getdatelist();
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  chooseNav(e){
    this.setData({
      nindex:e.currentTarget.dataset.current,
      date:e.currentTarget.dataset.date
    })
    this.gettimelist(this.data.date);
  },
  chosetime(e){
    var check = e.currentTarget.dataset.check;
    var date = e.currentTarget.dataset.date;
    var time = e.currentTarget.dataset.time;
    var that = this;
    let param = {
      scoure: 'wxapp',
      token:that.data.token,
      date:date,
      time:time
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.chosetime, param, url.REQUEST_METHOD_GET).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      wx.showToast({
        title: res.data.msg,
        icon:"none"
      })
      if(res.data.code==1){
          that.gettimelist(date);
       }
    })  	
  },
  getdatelist:function(){
    var that = this;
    let param = {
      scoure: 'wxapp',
      token:that.data.token
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.getdatelist, param, url.REQUEST_METHOD_GET).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      if(res.data.code==1){
         that.setData({
            datelist : res.data.data,
         })
         that.gettimelist(res.data.data[0].date);
       }
    })  	
  },
  gettimelist(date){
    var that = this;
    let param = {
      scoure: 'wxapp',
      token:that.data.token,
      date:date
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.workerschedule, param, url.REQUEST_METHOD_GET).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      if(res.data.code==1){
         that.setData({
             timelist : res.data.data,
         })
       }
    })  	
  },
  goSetting(){
    wx.navigateTo({
      url:'../working_hours/working_hours'
    })
  }
})